/*******************************************************************
REPLICATION FILE FOR:
Paper: “Class, Ethnicity, and Cooperation Among Women: Evidence from a Public Goods Experiment in Lebanon”
Authors: Leslie Marshall and Laura Paler

This do file does all cleaning and recoding following the 10 rounds of missing data imputation for the original data (see README).
It also creates all indices used in the analysis using inverse covariance weighting as in
Anderson (2008). It calls the make_index.ado file (written by Cyrus Samii, NYU) to create the indices.
********************************************************************/

*******************************************************
*SET GLOBAL DIRECTORIES
*******************************************************

*Set your personal working directory here: cd~
	cd ~/

	global dos "DOs_and_ADOs"
	global packages "DOs_and_ADOs/PACKAGES"

	global data_orig "DATA/ORIG"
	global data_newimp "DATA/NEW_IMPUTE"
	global working "DATA/NEW_IMPUTE/WORKING"

	global results "RESULTS" 
	global results_figs "RESULTS/FIGS"
	global results_tables "RESULTS/TABLES"
	
	set more off

*Need this to make indices below	
	do "$dos/make_index.ado"

	
*Call the dataset used in the actual analysis	
*Note that users who want to perform the analysis on the "new" data (see the README) can replace the line below with:  use "$data_newimp/Leb_2_Impute_FINAL_New.dta", clear
	use "$data_orig/Leb_PGG_2_Impute_FINAL.dta", clear

*******************************************************
*KEEP ONLY VARS USED IN ANALYSIS
*******************************************************

keep set gid pid ses sect strata_final final_actual treat_prob T_mixed_sect T_mixed_class ///
		strata_agg_FINAL2 strata_agg_FINAL final_assign ///
		scr_q8_sect scr_recruiter_id ///
		scr_q1_gender scr_q2_age scr_q3_marital scr_q4_educ scr_q6_work ///
		scr_q9_assets scr_q10_hh_area scr_q11_summer_hh scr_q12_poweralt scr_q13_vacay ///
		scr_q14_dineout scr_q15_income ///
		scr_q16_income_subj scr_econ_status /// 
		scr_q5_regdistrict scr_q5_regdistrict_REC1 ///
		pre_q15_a_concern_1 pre_q15_b_concern_2 pre_q15_c_concern_3 pre_q3_sect ///
		pre_q1_gender pre_q2_age pre_q9_class pre_q8_income pre_q10_diffecon pre_q11_a_zaimhelp pre_q11_b_relhelp ///
		pre_q4_changesect pre_q5_diffsect pre_q12_a_marry pre_q12_b_doctor pre_q12_c_neighbor ///
		pre_q12_d_poltalk pre_q12_e_econtalk pre_q12_f_supervisor pre_q12_g_friends ///
		pre_q7_a_friends pre_q7_b_elites pre_q7_c_petition pre_q7_d_protest pre_q13_disagree pre_q6_polparty ///
		pre_q14_a_agegrp pre_q14_b_gender pre_q14_c_confession pre_q14_d_class pre_q14_e_lebanese pre_q14_f_occupation ///
		post_q7_knowppl ///
		pgg_rd1 pgg_rd2 ew_eachearn ew_eachearn_REC2 ew_potshare ew_totalearn ew_totalearn_REC2 ///
		pgg_rd1_totalpot pgg_rd2_totalpot pgg_rdcounts cc_pos cc_neg ///
		wgt_strat2 wgt_strat3_particip ms_q1_mod_id ///
		ms_q4_date ///
		scr_q1_gender_REC2 scr_q3_marital_REC1 scr_q4_educ_REC1 scr_q6_work_REC1 scr_q9_assets_REC1 ///
		scr_q14_dineout_REC1 scr_q16_income_subj_REC1 ///
		pre_q15_a_concern_1_REC1 pre_q15_b_concern_2_REC1 pre_q15_c_concern_3_REC1 ///
		pre_q3_sect_REC3 pre_q9_class_REC1 pre_q9_class_REC3b ///
		pre_q1_gender_REC2 pre_q8_income_REC1 pre_q10_diffecon_REC1 pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1 ///
		pre_q4_changesect_REC1 pre_q5_diffsect_REC1 pre_q12_a_marry_REC1 pre_q12_b_doctor_REC1 pre_q12_c_neighbor_REC1 ///
		pre_q12_d_poltalk_REC1 pre_q12_e_econtalk_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 pre_q14_f_occupation_REC1 ///
		pre_q7_a_friends_REC1 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 pre_q13_disagree_REC1 ///
		pre_q14_a_agegrp_REC1 pre_q14_b_gender_REC1 pre_q14_c_confession_REC1 pre_q14_d_class_REC1 pre_q14_e_lebanese_REC1 ///
		pre_q6_polparty_REC2 pre_q6_polparty_REC3 ///
		post_q12_marriage post_q12_marriage_REC1 ///
		post_q7_knowppl_REC1 ///
		m_impute _mi_id _mi_miss _mi_m _mi _mj n_gid
			
/*******************************************************
*RENAME WEIGHTS
	wgt1 = 1 (for the sample)
	wgt4 = 'small' strata weight (accounts for uneq prob assignment within blocks and weights sample to 'population' assigned
	wgt6 = 'big' strata weight (weights sample to 'pop' assigned, eq prob assignment within blocks)
*******************************************************/
	
	g wgt1 = 1
	g wgt4 = wgt_strat2
	g wgt6 = wgt_strat3_particip
		
			
*******************************************************
*SCREENING SURVEY
*******************************************************

*Socio-economic status (X)
	g poor = scr_econ_status==0
	g rich = scr_econ_status==1

*Marital status	(X)
	recode scr_q3_marital_REC1 (2=1) (1 4 5=0), g(scr_q3_marital_REC2)
	lab def MARR 0 "Not married" 1 "Married", modify
	lab val scr_q3_marital_REC2 MARR
	g married =  scr_q3_marital_REC2
	lab var scr_q3_marital_REC2 "Marital status"

	
*Education (at least high school, includes technical degree) (X)
	recode scr_q4_educ_REC1 (4/6=0) (7/10=1), g(scr_q4_educ_REC2)	
	lab def EDUC 0 "Secondary or Less" 1 "Post-secondary", modify
	lab val scr_q4_educ_REC2 EDUC
	lab var scr_q4_educ_REC2 "Education"
	
*Sect/Confession	
	g pre_q3_christian = scr_q8_sect==1
	g pre_q3_sunni = scr_q8_sect==2
	g pre_q3_shia = scr_q8_sect==3	
	lab var pre_q3_christian "Christian"
	lab var pre_q3_sunni "Sunni"
	lab var pre_q3_shia "Shia"

*Work status -- binary indicator for "homemaker" and for "student" (X)
	gen homemaker=1 if scr_q6_work==5
	recode homemaker .=0 
	lab def HOME 0 "Other" 1 "Homemaker", modify
	lab val homemaker HOME
	
	gen student=1 if scr_q6_work==6
	recode student .=0 
	lab def STU 0 "Other" 1 "Student", modify
	lab val student STU	

	recode scr_q6_work_REC1 (2/3=1) (1 4 = 0), g(employed)
	
*Moderator indicator (X)
	g mod1 = ms_q1_mod_id==10
	lab var mod1 "Moderator 1 (of 2)"
	
*Variable labels
	lab var scr_q6_work_REC1 "Work status"
	lab var scr_q9_assets_REC1 "Assets (screening)"
	lab var scr_q10_hh_area "HH area (screening)"
	lab var scr_q11_summer_hh "Summer house (screening)"
	lab var scr_q12_poweralt "Electricity (screening)"
	lab var scr_q13_vacay "Vacation (screening)"
	lab var scr_q14_dineout_REC1 "Dineout (screening)"
	lab var scr_q15_income "Household income (screening)"
	lab var scr_q16_income_subj_REC1 "Income subjective (screening)"
	lab var n_gid "Groups with six participants"
	lab def SCR_Q5_R 1 "Beirut/Mt. Leb" 2 "North/Bekaa" 3 "Nabatiye/South" 
	lab val scr_q5_regdistrict_REC1 SCR_Q5_R
	
	
*******************************************************
*PRE-TREATMENT SURVEY (AND INDICES)
*******************************************************

	lab var T_mixed_class "Mixed class group"
	lab var T_mixed_sect "Mixed sect group"		
	
*Gender
	ren pre_q1_gender_REC2 female
	lab def SEX 0 "Male" 1 "Female", modify
	lab val female SEX
	lab var female "Female"	
	g male = female==0	
	
*Age
	lab var pre_q2_age "Age"
	
	
*Politically engaged index	
	local c_act pre_q7_a_friends_REC1 pre_q7_b_elites_REC1 pre_q7_c_petition_REC1 pre_q7_d_protest_REC1 

	foreach item in c_act {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}


*Economic status index
	recode pre_q9_class_REC1 (5=1) (4=2) (3=3) (2=4) (1=5), g(pre_q9_class_REC1b)
		
	local c_econ_all scr_q9_assets_REC1 scr_q10_hh_area scr_q11_summer_hh scr_q12_poweralt scr_q13_vacay scr_q14_dineout_REC1 scr_q15_income scr_q16_income_subj_REC1 pre_q8_income_REC1 pre_q9_class_REC3b 
					
	foreach item in c_econ_all {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}
	
	
*Network heterogeneity index	
	local c_soc_hom pre_q10_diffecon_REC1 pre_q5_diffsect_REC1
	
	foreach item in c_soc_hom {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}
	
		
*pre_q10_diffecon_REC3 -- homogeneous coded high
	foreach var of varlist pre_q10_diffecon pre_q5_diffsect {  											
		recode `var'_REC1 (1/2=0) (3=1) (4/5=2), g(`var'_REC3) 		
		}


	tab pre_q10_diffecon_REC3, g(pre_q10_diffecon_REC3_DUM)

	lab def class_homog1 1 "Almost all from diff class" 2 "Most from diff class" 3 "Some from diff class" 4 "A few from diff class" 5 "Almost none from diff class", modify	
	lab val pre_q10_diffecon_REC1 class_homog1 

	lab def sect_homog1 1 "Almost all from diff sect" 2 "Most from diff sect" 3 "Some from diff sect" 4 "A few from diff sect" 5 "Almost none from diff sect", modify	
	lab val pre_q5_diffsect_REC1 sect_homog1

	
*Connected index 
	local connect pre_q11_a_zaimhelp_REC1 pre_q11_b_relhelp_REC1

	foreach item in connect {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}
	lab def PRE_Q11 1 "Very difficult" 2 "Somewhat difficult" 3 "Not too difficult" 4 "Not difficult at all", modify
	lab val pre_q11_a_zaimhelp_REC1 PRE_Q11
	lab val pre_q11_b_relhelp_REC1 PRE_Q11
	lab var pre_q11_a_zaimhelp_REC1 "Access to benefits through Zaim/Politicians"
	lab var pre_q11_b_relhelp_REC1 "Access to benefits through Religious Leaders"

	
*Prejudice index	
	local c_prej pre_q12_a_marry_REC1 pre_q12_b_doctor_REC1 pre_q12_c_neighbor_REC1 pre_q12_d_poltalk_REC1 pre_q12_e_econtalk_REC1 pre_q12_f_supervisor_REC1 pre_q12_g_friends_REC1 
	
	foreach item in c_prej {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}	
	
*Sectarian identity index
	recode pre_q6_polparty_REC3 (1=3) (2=2) (3=1), g(pre_q6_polparty_REC3b)

	recode pre_q14_c_confession_REC1 (7=1) (6=2) (5=3) (4=4) (3=5) (2=6) (1=7), g(pre_q14_c_confession_REC1b)
	
	local c_sect_id pre_q4_changesect_REC1 pre_q6_polparty_REC2 pre_q14_c_confession_REC1b	
	
	foreach item in c_sect_id {
		foreach weight in wgt1 wgt4 wgt6 {
			mi xeq: make_index `item'_`weight' `weight' ``item''
		}
	}	
	

*Q14 Identification
*Reverse the scale so that strong identity is coded high	
*Class ID
	recode pre_q14_d_class_REC1 (7=1) (6=2) (5=3) (4=4) (3=5) (2=6) (1=7), g(pre_q14_d_class_REC1b)
	lab var pre_q14_d_class_REC1b "Relative strength of class ID (strong coded high)"

*Occupational ID	
	recode pre_q14_f_occupation_REC1 (7=1) (6=2) (5=3) (4=4) (3=5) (2=6) (1=7), g(pre_q14_f_occupation_REC1b)
		lab var pre_q14_f_occupation_REC1b "Relative strength of occupation id (strong coded high)"
	
*Age group ID	
	recode pre_q14_a_agegrp_REC1 (1=7) (7=1) (2=6) (6=2) (3=5) (5=3) (4=4), g(pre_q14_a_agegrp_REC1b)
		lab var pre_q14_a_agegrp_REC1b "Relative strength of age group id (strong coded high)"

*Gender ID (multiple codings used in analysis)		
	recode pre_q14_b_gender_REC1 (1=7) (7=1) (2=6) (6=2) (3=5) (5=3) (4=4), g(pre_q14_b_gender_REC1b)
	lab var pre_q14_b_gender_REC1b	"Relative strength of gender ID (strong coded high)"

	recode pre_q14_b_gender_REC1b (7=1) (1/6=0), g(pre_q14_b_gender_REC2)
	recode pre_q14_b_gender_REC1b (5/7=1) (1/4=0), g(pre_q14_gender_top3)

*Confessional ID	
	lab var pre_q14_c_confession_REC1b	"Relative strength of sect ID (strong coded high)"

*Lebanese ID
	recode pre_q14_e_lebanese_REC1 (7=1) (6=2) (5=3) (4=4) (3=5) (2=6) (1=7), g(pre_q14_leb_REC1b)
	lab var pre_q14_leb_REC1b "Relative strength of Lebanese ID (strong coded high)"

	
*Q15 PRIORITIES 
	recode pre_q15_a_concern_1_REC1 (1=1) (2=2) (18=3) (22=4) (27=5) (3/12=6) (13/17 19/21=7) (23/26=8) (28/30=9), g(pre_q15_concern_1_main)
		lab def pre_q15_concern_1_main 1 "Prices" 2 "Unemployment" 3 "Sectarianism" 4 "Civil War" 5 "Terrorism" 6 "Other Econ" 7 "Other Social" 8 "Other Security" 9 "Other", modify
		lab val pre_q15_concern_1_main pre_q15_concern_1_main

*Economic concerns	
	g concern_prices_all = pre_q15_a_concern_1_REC1==1 |  pre_q15_b_concern_2_REC1==1 | pre_q15_c_concern_3_REC1 == 1
	g concern_unemp_all = pre_q15_a_concern_1_REC1==2 |  pre_q15_b_concern_2_REC1==2 | pre_q15_c_concern_3_REC1 == 2
	

*Other pre-treatment survey variable labels	
	lab var pre_q13_disagree_REC1 "How often do you discuss when disagree"
	lab var pre_q8_income_REC1 "Household income (pre-treatment)"
	lab var pre_q9_class_REC3b "Self-identified class (pre-treatment)"
	lab var pre_q12_a_marry_REC1 "Marrying someone from a diff confession"
	lab var pre_q12_b_doctor_REC1 "Diff confession as physician"
	lab var pre_q12_c_neighbor_REC1 "Diff confession as neighbor"
	lab var pre_q12_d_poltalk_REC1 "Discussing politics with diff confession"
	lab var pre_q12_e_econtalk_REC1 "Discussing social or econ issues with diff confession"
	lab var pre_q12_f_supervisor_REC1 "Supervised by diff confession"
	lab var pre_q12_g_friends_REC1 "Friends with diff confession"
	lab var pre_q7_a_friends_REC1 "Discuss issues"
	lab var pre_q7_b_elites_REC1 "Talked to party members"
	lab var pre_q7_c_petition_REC1 "Signed a petition"
	lab var pre_q7_d_protest_REC1 "Attended protest"
	lab var pre_q10_diffecon_REC1 "Friends from same class"
	lab var pre_q5_diffsect_REC1 "Friends from same sect"
	lab var pre_q4_changesect_REC1 "Willing to change sect"
	lab var pre_q6_polparty_REC3b "Support sectarian political party"
	lab var pre_q6_polparty_REC2 "Supports a sectarian political party"
					
	lab def pre_q10_REC1 1 "Almost All" 2 "Most" 3 "Some" 4 "A Few" 5 "Almost None"
	lab val pre_q10_diffecon_REC1 pre_q10_REC1	
				
	lab var index_c_econ_all_wgt4 "Economic wealth index"
	lab var index_c_prej_wgt4 "Prejudice index"
	lab var index_c_act_wgt4 "Political action index"
	lab var index_c_soc_hom_wgt4 "Social homogeneity index"
	lab var index_c_sect_id_wgt4 "Sectarian identity index"
	

*******************************************************
*POST TREATMENT
*******************************************************

*Other post-treatment survey variable labels	
	lab var post_q7_knowppl_REC1 "Knew people in group"
	
*Post-treatment survey -- marriage question
	recode post_q12_marriage_REC1 (1/2=1) (3/4=0), g(post_q12_marriage_REC2b)


*******************************************************
*PUBLIC GOODS GAME
*******************************************************

*Comprehension of the payoff structure in the public goods game
	g pgg_prac_prob = 1 if ew_eachearn_REC2==1 & ew_potshare==1 & ew_totalearn_REC2==1 
	recode pgg_prac_prob (.=0)
	lab val pgg_prac_prob BIN			
	lab var pgg_prac_prob "Answered all Practice Problem Qs correctly"
		
* calculate amount paid out to each group member from the total contribution to the group pot (first multiple by 1.5 & then divide by total group members)
	* as if round 1 counts:
	g pgg_tot_rd1 = ((pgg_rd1_totalpot*1.5)/6) if n_gid==1 & m_impute==0
	replace pgg_tot_rd1 = ((pgg_rd1_totalpot*1.5)/5) if n_gid==0 & m_impute==0
	tab pgg_tot_rd1 if m_impute==0
	sum pgg_tot_rd1 if m_impute==0

* calculate amount in private account for each individual (what they kept out of 10,000)
	* round 1 only
	g pgg_priv_rd1 = 10000-pgg_rd1 if m_impute==0
	tab pgg_priv_rd1 if m_impute==0, miss

* combine what's in private account with amount paid out by the public good (group pot)
	* as if round 1 counts:
	g pgg_earn_rd1 = pgg_tot_rd1+pgg_priv_rd1 if m_impute==0
	tab pgg_earn_rd1 if m_impute==0, miss
	sum pgg_earn_rd1 if m_impute==0

* calculate amount paid out to each group member from the total contribution to the group pot (first multiple by 1.5 & then divide by total group members)
	* as if round 2 counts:
	g pgg_tot_rd2 = ((pgg_rd2_totalpot*1.5)/6) if n_gid==1 & m_impute==0
	replace pgg_tot_rd2 = ((pgg_rd2_totalpot*1.5)/5) if n_gid==0 & m_impute==0
	tab pgg_tot_rd2 if m_impute==0

* calculate amount in private account for each individual (what they kept out of 10,000)
	* round 2 only
	g pgg_priv_rd2 = 10000-pgg_rd2 if m_impute==0
	tab pgg_priv_rd2 if m_impute==0, miss

* combine what's in private account with amount paid out by the public good (group pot)
	* as if round 1 counts:
	g pgg_earn_rd2 = pgg_tot_rd2+pgg_priv_rd2 if m_impute==0
	tab pgg_earn_rd2 if m_impute==0, miss
	sum pgg_earn_rd2 if m_impute==0

*** now combine answers for total earned in the PGG depending on which round actually counted (coin flip)
	g pgg_totearn = .
	replace pgg_totearn = pgg_earn_rd1 if pgg_rdcounts==1 & m_impute==0
	replace pgg_totearn = pgg_earn_rd2 if pgg_rdcounts==2 & m_impute==0
	sum pgg_totearn if m_impute==0, detail

	lab var pgg_totearn "Income earned from the public goods game"

* create a variable if the respondent is a conditional cooperator of any kind (positive or negative)
		g cond_coop = 1 if cc_pos==1 | cc_neg==1
		recode cond_coop (.=0)
		lab val cond_coop BIN
		lab var cond_coop "Conditional cooperator"	
	

*******************************************************
*MORE MISC VARS
*******************************************************

*Tag by imputation and gid	
	egen tag_gid = tag(m_impute gid)
	
	
*Create a control for weeks from election? (May 8, 2016)					
	g month = month(ms_q4_date)
	g day = day(ms_q4_date)					
	g days_to_elex = 158 - (month*30 + day)
		
	g all = 1
	
*Make weights figure compatible for descriptive stats figures		
	foreach var of varlist wgt1 wgt4 wgt6 {
				g double `var'_fig=round(`var'*1000000)
				codebook `var'_fig
					}
				
	lab var wgt1_fig "wgt1 for use as fweights in figures"
	lab var wgt4_fig "wgt4 for use as fweights in figures"
	lab var wgt6_fig "wgt6 for use as fweights in figures"
					
						
/*******************************************************
SAVE DATA
*******************************************************/
			
				
save "$data_orig/Leb_PGG_3_Clean_FINAL.dta", replace

